package com.fs.web.yz.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fs.common.pojo.CheckDetail;
import com.fs.common.pojo.CheckNote;
import com.fs.common.pojo.User;
import com.fs.common.service.CheckNoteService;
import com.fs.common.vo.*;
import com.fs.common.yzMapper.CheckDetailMapper;
import com.fs.common.yzMapper.CheckNoteMapper;
import com.fs.web.base.enums.CodeEnum;
import com.fs.web.base.param.SysConstant;
import com.fs.web.base.service.BaseService;
import com.fs.web.base.utils.ApplicationUtils;
import com.fs.web.base.utils.DateUtils;
import com.fs.web.base.vo.Criteria;
import com.fs.web.base.vo.DataGrid;
import com.fs.web.base.vo.JsonResponse;
import com.fs.web.yz.service.ICheckNoteService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author DeveloperKK
 * @desc 问卷管理
 */
@Slf4j
@Controller
@RequestMapping("/console/checkNote/rewrite")
public class CheckNoteController {

    @Autowired
    private BaseService baseService;
    @Autowired
    private CheckDetailMapper checkDetailMapper;
    @Autowired
    private CheckNoteMapper checkNoteMapper;

    @Autowired
    private CheckNoteService checkNoteService;

    @Autowired
    private ICheckNoteService iCheckNoteService;

    @RequestMapping("/viewRecipes")
    public String viewRecipes(){
        return "/console/checkNote/viewRecipes";
    }


    @ResponseBody
    @RequestMapping("/list")
    public JsonResponse list(HttpServletRequest request){
        try {
            DataGrid grid = new DataGrid(request);
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            User user = ApplicationUtils.getUser().getUser();
            if (user.isSysAdm()){
                map = null; // 如果是管理员,查询条件为null,查询全部信息
            }
            QueryWrapper<Object> wrapper = new Criteria<Object>().addAuto(map);
            IPage page = baseService.page("com.fs.common.pojo.CheckNote", request, wrapper);
            grid = DataGrid.gridBuilder(page);
            // return JsonResponse.success(convert(grid));    // 原代码做了一层转换方法,作用未知 1.暂时修改为无转换返回,使页面可以加载数据
            return JsonResponse.success(grid);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }

    /**
     * 根据日期获取问卷数量
     * @return
     */
    @ResponseBody
    @RequestMapping("/countForDate")
    public JsonResponse countForDate(){
        String beforeDay = DateUtils.beforeDay(-7);
        List<String> dayList = DateUtils.intervalDay(beforeDay, new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()));
        List<Map> list = iCheckNoteService.getCountForDate(dayList);

        return JsonResponse.success(list);
    }

    /**
     * 获取地图检测分布信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAddressCount")
    public JsonResponse test(){
        return JsonResponse.success(checkNoteService.getAddressCount());
    }



    /**
     *
     * @param checkId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getRecipes")
    @ApiOperation(value = "获取问卷信息")
    public JsonResponse list(String checkId){
        try {
            if (StringUtils.isNotBlank(checkId)) {
                CheckNote checkNote = checkNoteMapper.selectById(checkId);
                List<WeekRecipesVo> recipes = checkNoteService.getRecipes(checkId);
                Map<String, ReportVo> reportMap = checkNoteService.parseReport(checkNote);
                ScoreVo score = JSON.parseObject(checkNote.getCheckScore(),ScoreVo.class);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("checkNote",checkNote);
                jsonObject.put("recipes",recipes);
                jsonObject.put("score",score);
                jsonObject.put("report",reportMap);
                return JsonResponse.success(jsonObject);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return JsonResponse.error(CodeEnum.EX999);
    }


    private DataGrid convert(DataGrid grid){
        List<CheckNote> checkNoteList = new ArrayList<>();
        List<CheckNote> rows = (List<CheckNote>)grid.getRows();
        for (CheckNote ct : rows) {
            CheckDetail cd = checkDetailMapper.findByCheckId(ct.getCheckId());
            if (cd != null) {
               String result = cd.getCheckWords();
               if (StringUtils.isNotBlank(result)) {
                   String desc = "";
                    for (String code : result.split("#")) {
                        desc +=  SysConstant.DISEASE_CODE_NAME.get(code) + " ";
                    }
                   ct.setCheckJson(desc);
                   desc = "";
               }
            }
            checkNoteList.add(ct);
        }
        grid.setRows(checkNoteList);
        return grid;
    }


}
